package com.example.bilibili.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.bilibili.Test.test2;
import com.example.bilibili.domain.BiliUser;
import com.example.bilibili.domain.BiliVideo;
import com.example.bilibili.service.BiliUserService;
import com.example.bilibili.service.BiliVideoService;
import com.example.bilibili.util.UpLoad1;
import com.example.bilibili.util.upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class UploadController {

    private com.example.bilibili.util.upload upload;
    private BiliVideo biliVideo;
    private test2 test;
    private List<String> list;
    private String vidname;
    private String imgname;
    private UpLoad1 upload1;
    private String userimgname;

    @Autowired
    private BiliVideoService biliVideoService;
    @Autowired
    private BiliUserService biliUserService;
    @RequestMapping("/uploadvideo")
    public Object uploadvideo( String videotitle, String videoIntrbriefing, Long uploaderid,  MultipartFile file1, MultipartFile file2) throws IOException {
       biliVideo=new BiliVideo();
        upload=new upload();
        biliVideo.setVideoid(String.valueOf((int)((Math.random()*9+1)*100000)));
        biliVideo.setAuditingid(7);
        biliVideo.setVideotitle(videotitle);
        biliVideo.setVideointrbriefing(videoIntrbriefing);
        biliVideo.setUploaderid(uploaderid);

        InputStream inputStream1 = null;
        File filea = null;
        filea = File.createTempFile("temp", null);
        file1.transferTo(filea);   //sourceFile为传入的MultipartFile
        inputStream1 = new FileInputStream(filea);
/////////////////////////////////////////////////
        InputStream inputStream2 = null;
        File fileb = null;
        fileb = File.createTempFile("temp", null);
        file2.transferTo(fileb);   //sourceFile为传入的MultipartFile
        inputStream2 = new FileInputStream(fileb);
        List<String> list =new ArrayList<String>();
        list=upload.upload(inputStream1,inputStream2);
        biliVideo.setVideopath(list.get(0));
        biliVideo.setCoverimage(list.get(1));
        biliVideoService.save(biliVideo);
        return 1;
    }
    @RequestMapping("/uploadvideo2")
    public Object uploadvideo2(@RequestParam String videotitle,@RequestParam String videoIntrbriefing,@RequestParam Long uploaderid,@RequestParam MultipartFile file1) throws IOException {
        biliVideo=new BiliVideo();
        upload=new upload();
        biliVideo.setVideoid(String.valueOf((int)((Math.random()*9+1)*100000)));
        biliVideo.setAuditingid(7);
        biliVideo.setVideotitle(videotitle);
        biliVideo.setVideointrbriefing(videoIntrbriefing);
        biliVideo.setUploaderid(uploaderid);

        InputStream inputStream1 = null;
        File filea = null;
        filea = File.createTempFile("temp", null);
        file1.transferTo(filea);   //sourceFile为传入的MultipartFile
        inputStream1 = new FileInputStream(filea);
        List<String> list =new ArrayList<String>();
        list=upload.upload(inputStream1,null);
        biliVideo.setVideopath(list.get(0));

        biliVideoService.save(biliVideo);
        return 1;
    }

    @RequestMapping("/uploadvideo3")
    public Object uploadvideo3(@RequestParam MultipartFile file) throws IOException {
       //test=new test2();

        upload1=new UpLoad1();
        InputStream inputStream1 = null;
        File filea = null;
        filea = File.createTempFile("temp", null);
        file.transferTo(filea);   //sourceFile为传入的MultipartFile
        inputStream1 = new FileInputStream(filea);

        upload1.upload1(inputStream1,"test","video/");
       //test.test(inputStream1);
        return 1;
    }
    @RequestMapping("/uploadvideo4")
    public Object uploadvideo4( String videotitle, String videoIntrbriefing, Long uploaderid) {
        biliVideo=new BiliVideo();
        upload=new upload();
        String videoid="AV"+String.valueOf((int)((Math.random()*9+1)*100000));
        biliVideo.setVideoid(videoid);
        biliVideo.setAuditingid(5);
        biliVideo.setVideotitle(videotitle);
        biliVideo.setVideointrbriefing(videoIntrbriefing);
        biliVideo.setUploaderid(uploaderid);
        this.vidname=videoid+".mp4";
        this.imgname=videoid+".png";
        biliVideo.setVideopath("https://" + "upload1314." + "oss-cn-qingdao.aliyuncs.com"+ "/video/"+vidname);
        biliVideo.setCoverimage("https://" + "upload1314." + "oss-cn-qingdao.aliyuncs.com"+ "/img/"+imgname);
        biliVideoService.save(biliVideo);
        return 1;
    }
    @RequestMapping("/uploadvideo5")
    public Object uploadvideo5(@RequestParam MultipartFile file) throws IOException {

        upload1=new UpLoad1();
        InputStream inputStream1 = null;
        File filea = null;
        filea = File.createTempFile("temp", null);
        file.transferTo(filea);   //sourceFile为传入的MultipartFile
        inputStream1 = new FileInputStream(filea);
/////////////////////////////////////////////////
        upload1.upload1(inputStream1,vidname,"video/");
        return 1;
    }
    @RequestMapping("/uploadvideo6")
    public Object uploadvideo6(@RequestParam MultipartFile file) throws IOException {

        upload1=new UpLoad1();
        InputStream inputStream1 = null;
        File filea = null;
        filea = File.createTempFile("temp", null);
        file.transferTo(filea);   //sourceFile为传入的MultipartFile
        inputStream1 = new FileInputStream(filea);
/////////////////////////////////////////////////

        return upload1.upload1(inputStream1,imgname,"img/");
    }

    @RequestMapping("/updateuserinfo")
    public int updateuserinfo(long userid,String userauthor,Integer gender,String phone,String userautograph){
        UpdateWrapper qw= new UpdateWrapper();
        qw.eq("userId",userid);
        BiliUser user= biliUserService.getOne(qw);
        if(user==null)
            return 0;
        user.setUserauthor(userauthor);
        user.setGender(gender);
        user.setPhone(phone);
        user.setUserautograph(userautograph);
        userimgname=user.getUsername()+".png";
        user.setUseravatar("https://" + "upload1314." + "oss-cn-qingdao.aliyuncs.com"+ "/userimg/"+userimgname);
        return biliUserService.update(user,qw)?1:0;
    }
    @RequestMapping("/uploaduserimg")
    public Object uploaduserimg(@RequestParam MultipartFile file) throws IOException {
          // String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        upload1=new UpLoad1();
        InputStream inputStream1 = null;
        File filea = null;
        filea = File.createTempFile("temp", null);
        file.transferTo(filea);   //sourceFile为传入的MultipartFile
        inputStream1 = new FileInputStream(filea);
/////////////////////////////////////////////////

        return upload1.upload1(inputStream1,userimgname,"userimg/");
    }
}
